
package com.idsmanager.zdh.service.business.portal;


import com.idsmanager.commons.web.WebUtils;
import com.idsmanager.commons.web.context.BeanProvider;
import com.idsmanager.oauth.domain.security.SecurityUtils;
import com.idsmanager.oauth.domain.security.User;
import com.idsmanager.oauth.domain.security.UserRepository;
import com.idsmanager.oauth.service.dto.user.UserEditTableDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserTableDtoUpdater {


    private static final Logger LOG = LoggerFactory.getLogger(UserTableDtoUpdater.class);

    private transient UserRepository userRepository = BeanProvider.getBean(UserRepository.class);

    private UserEditTableDto userEditTableDto;

    public UserTableDtoUpdater(UserEditTableDto userEditTableDto) {
        this.userEditTableDto = userEditTableDto;
    }

    public String update() {
        User user = userRepository.findByGuid(userEditTableDto.getUuid());
        if(user == null){
            LOG.warn("{}|Not found user by uuid:{}", WebUtils.getIp(),userEditTableDto.getUuid());
            return null;
        }
        user.setUsername(userEditTableDto.getUsername());
        user.setPrivileges(userEditTableDto.getPrivileges());
        userRepository.updateUser(user);
        LOG.debug("{}|update User:{} ", SecurityUtils.username(),user);
        return user.uuid();
    }
}
